package com.mx.zb.wxhub.job;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.mx.zb.wxhub.service.WxTicketService;

import net.minidev.json.JSONObject;


@Component
public class TokenScheduler {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass()); 
	
	private static final String component_token_url="https://api.weixin.qq.com/cgi-bin/component/api_component_token";	
	
	@Autowired  
    RestTemplate rest;

//	@Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次  
//    public void statusCheck() {      
//        logger.info("每分钟执行一次。开始……");  
//        //statusTask.healthCheck();  
//        logger.info("每分钟执行一次。结束。");  
//    }    
//  
//    @Scheduled(fixedRate=20000)  
//    public void testTasks() {      
//        logger.info("每20秒执行一次。开始……");  
//        //statusTask.healthCheck();  
//        logger.info("每20秒执行一次。结束。");  
//    }
    
    /**
     * 每110分钟获取一次第三方的TOKEN，过期是2小时
     */
    @Scheduled(fixedRate=1000*60*110)  
    public void getComponentAccessToken() {      
        
    	if(WxTicketService.componentVerifyTicket!=null) {
    		JSONObject postData = new JSONObject();  
        	
            postData.put("component_appid", WxTicketService.AppID);
            postData.put("component_appsecret", WxTicketService.AppSecret);
            postData.put("component_verify_ticket", WxTicketService.componentVerifyTicket);
            
            JSONObject json = rest.postForEntity(component_token_url, postData, JSONObject.class).getBody(); 
            
            
            
    	}
    	
    	
    }
    
}
